﻿// 1.1.7.14. Write a program, which finds the longest sequence of equal string elements in a matrix.
// A sequence in a matrix we define as a set of neighbor elements on the same row, column or diagonal.
// Examples:  ha fifi ho hi                    s qq s
//            fo  ha  hi xx -> ha, ha, ha     pp pp s -> s, s, s
//           xxx  ho  ha xx                   pp pp s

using System;

class LongestSequenceOfEqualStrings
{
    static void Main()
    {
        string[,] matrice = new string[3, 4]
        {
            { "ha", "fifi", "ho", "hi" },
            { "fo", "ha", "hi", "xx" },
            { "xxx", "ho", "ha", "xx" }
        };
        int longestLength = 0;
        int endSequenceX = 0;
        int endSequenceY = 0;
        string type = null;
        for (int row = 0; row < matrice.GetLength(0) - 1; row++)
        {
            int currentLength = 0;
            for (int col = row + 1; col < matrice.GetLength(1); col++)
            {
                if (matrice[row, col] == matrice[row, col - 1])
                {
                    currentLength++;
                    if (currentLength > longestLength)
                    {
                        longestLength = currentLength;
                        endSequenceX = col;
                        endSequenceY = row;
                        type = "horizontalen";
                    }
                }
            }
        }
        for (int col = 0; col < matrice.GetLength(1) - 1; col++)
        {
            int currentLength = 0;
            for (int row = col + 1; row < matrice.GetLength(0); row++)
            {
                if (matrice[row, col] == matrice[row - 1, col])
                {
                    currentLength++;
                    if (currentLength > longestLength)
                    {
                        longestLength = currentLength;
                        endSequenceX = col;
                        endSequenceY = row;
                        type = "vertikalen";
                    }
                }
            }
        }
        int startPositionX = 0;
        int startPositionY = matrice.GetLength(0) - 1;
        int stepper = 1;
        while (stepper <= matrice.GetLength(0) * matrice.GetLength(1))
        {
            int currentPositionX = startPositionX;
            int currentPositionY = startPositionY;
            string previusElement = matrice[startPositionY, startPositionX];
            int currentLength = 0;
            while (currentPositionX < matrice.GetLength(1) && currentPositionY < matrice.GetLength(0))
            {
                stepper++;
                string currentElement = matrice[currentPositionY, currentPositionX];
                if (currentElement == previusElement)
                {
                    currentLength++;
                    if (currentLength > longestLength)
                    {
                        longestLength = currentLength;
                        endSequenceX = currentPositionX;
                        endSequenceY = currentPositionY;
                        type = "nadqsnoInadolu";
                    }
                }
                previusElement = matrice[currentPositionY, currentPositionX];
                currentPositionX++;
                currentPositionY++;
                
            }
            if (startPositionY != 0)
            {
                startPositionY--;
            }
            else
            {
                startPositionX++;
            }
        }


        startPositionX = matrice.GetLength(1) - 1;
        startPositionY = matrice.GetLength(0) - 1;
        stepper = 1;
        while (stepper <= matrice.GetLength(0) * matrice.GetLength(1))
        {
            int currentPositionX = startPositionX;
            int currentPositionY = startPositionY;
            string previusElement = matrice[startPositionY, startPositionX];
            int currentLength = 0;
            while (currentPositionX >= 0 && currentPositionY < matrice.GetLength(0))
            {
                stepper++;
                string currentElement = matrice[currentPositionY, currentPositionX];
                if (currentElement == previusElement)
                {
                    currentLength++;
                    if (currentLength > longestLength)
                    {
                        longestLength = currentLength;
                        endSequenceX = currentPositionX;
                        endSequenceY = currentPositionY;
                        type = "nalqvoInadolu";
                    }
                }
                previusElement = matrice[currentPositionY, currentPositionX];
                currentPositionX--;
                currentPositionY++;

            }
            if (startPositionY != 0)
            {
                startPositionY--;
            }
            else
            {
                startPositionX--;
            }
        }
        if (type == "horizontalen")
        {
            for (int i = 0; i < longestLength; i++)
            {
                Console.WriteLine(matrice[endSequenceY, endSequenceX]);
                endSequenceX--;
            }
        }
        else if (type == "vertikalen")
        {
            for (int i = 0; i < longestLength; i++)
            {
                Console.WriteLine(matrice[endSequenceY, endSequenceX]);
                endSequenceY--;
            }
        }
        else if (type == "nadqsnoInadolu")
        {
            for (int i = 0; i < longestLength; i++)
            {
                Console.WriteLine(matrice[endSequenceY, endSequenceX]);
                endSequenceX--;
                endSequenceY--;
            } 
        }
        else
        {
            for (int i = 0; i < longestLength; i++)
            {
                Console.WriteLine(matrice[endSequenceY, endSequenceX]);
                endSequenceX--;
                endSequenceY++;
            }
        }
    }
}
